// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2019-2025, The OpenROAD Authors

#pragma once

#include "dbCore.h"
#include "odb/dbId.h"
#include "odb/dbTypes.h"
#include "odb/dbViaParams.h"
#include "odb/geom.h"
#include "odb/odb.h"

namespace odb {

class _dbBox;
class _dbDatabase;
class _dbTech;
class _dbTechLayer;
class _dbTechViaGenerateRule;
class dbIStream;
class dbOStream;

struct _dbViaFlags
{
  uint _is_rotated : 1;
  uint _is_tech_via : 1;
  uint _has_params : 1;
  dbOrientType::Value _orient : 4;
  bool default_ : 1;
  uint _spare_bits : 24;
};

class _dbVia : public _dbObject
{
 public:
  // PERSISTANT-MEMBERS
  _dbViaFlags _flags;  // 5.6 DEF
  char* _name;
  char* _pattern;
  dbId<_dbBox> _bbox;  // Caching the bbox speeds up defin imports.
  dbId<_dbBox> _boxes;
  dbId<_dbTechLayer> _top;     // Caching the layer speeds up defin imports.
  dbId<_dbTechLayer> _bottom;  // Caching the layer speeds up defin imports.
  dbId<_dbTechViaGenerateRule>
      _generate_rule;    // via generated by tech-via-rule, 5.6 DEF
  uint _rotated_via_id;  // via-id that was roated to produce this via, 5.6 DEF
  _dbViaParams _via_params;  // params used to generate this via, 5.6 DEF

  _dbVia(_dbDatabase*, const _dbVia& v);
  _dbVia(_dbDatabase*);
  ~_dbVia();

  bool operator==(const _dbVia& rhs) const;
  bool operator!=(const _dbVia& rhs) const { return !operator==(rhs); }
  bool operator<(const _dbVia& rhs) const
  {
    return strcmp(_name, rhs._name) < 0;
  }

  _dbTech* getTech();
  void collectMemInfo(MemInfo& info);
};

dbOStream& operator<<(dbOStream& stream, const _dbVia& v);
dbIStream& operator>>(dbIStream& stream, _dbVia& v);

}  // namespace odb
